package progressBar;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import bls.juejin.cn.R;


public class MyProgressBar {
	Dialog dialog;
	Context context;
	//声明ProgressBar对象
	private ProgressBar pro1;
	private TextView tvTitle;

	/**
	 * 构造
	 */
	public MyProgressBar(Context context) {
		// TODO Auto-generated constructor stub
		this.context=context;
		dialog=new Dialog(context,R.style.dialog);
		dialog.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失
		dialog.setOnCancelListener(onCancelListener);
	}

	public void setPress(int presss) {
		pro1.setProgress(presss);
		if (presss==100) {
			tvTitle.setText("新版本下载完成，准备安装");
			colseDialog();

		}else {
			tvTitle.setText("正在下载新版本："+presss+"%");
		}
	}
	/**
	 * 初始化进度对话框
	 */
	public void initDialog(){

		LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View view=inflater.inflate(R.layout.my_progressbar, null);
		dialog.setContentView(view);
		pro1=(ProgressBar)dialog.findViewById(R.id.progressBar1);
		tvTitle=(TextView) dialog.findViewById(R.id.tv_title);
		//设置进度条是否自动旋转,即设置其不确定模式,false表示不自动旋转
		pro1.setIndeterminate(false);
		//设置ProgressBar的最大值
		pro1.setMax(100);

		//设置ProgressBar的当前值
		pro1.setProgress(0);

		dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
			@Override
			public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)
			{
				if (keyCode == KeyEvent.KEYCODE_SEARCH)
				{
					return true;
				}
				else
				{
					return true; //默认返回 false，这里false不能屏蔽返回键，改成true就可以了
				}
			}
		});
		dialog.show();
	}

	Handler handler=new Handler(){

		@Override
		public void handleMessage(Message msg) {



		}

	};
	public void colseDialog(){
		dialog.dismiss();
	}
	public boolean isShowing(){
		if(dialog.isShowing()){
			return true;
		}
		else{
			return false;
		}
	}
	OnCancelListener onCancelListener=new OnCancelListener() {
		@Override
		public void onCancel(DialogInterface dialog) {
			// TODO Auto-generated method stub			
			dialog.dismiss();
			System.out.println("cancel!!!!");
		}
	};

}
